#include "InetIpv4.h"

#include "PcapExceptions.h"
#include <arpa/inet.h>
#include <memory.h>


namespace Pacap {

PackType InetIpv4::pack()
{
    throw ExceptUndefineMethod("not defined method");
}

void InetIpv4::unpack(PackType hexData)
{
    if( hexData.length() < sizeof(struct head) ){
        throw ExceptUncorrectLength("can not allow length of hexData less then pack");
    }
    ::memcpy(&(this->head),hexData.data(),sizeof(struct head));
    uint32_t headLen = getIpHl();

    data.assign(hexData.data()+headLen,hexData.length()-headLen);
}

unsigned int InetIpv4::getIpHl() const
{
    return head.ipHl*4;
}

void InetIpv4::setIpHl(unsigned int value)
{
    head.ipHl = value;
}

unsigned int InetIpv4::getIpV() const
{
    return head.ipV;
}

void InetIpv4::setIpV(unsigned int value)
{
    head.ipV = value;
}

u_int8_t InetIpv4::getIpTos() const
{
    return head.ipTos;
}

void InetIpv4::setIpTos(const u_int8_t &value)
{
    head.ipTos = value;
}

u_short InetIpv4::getIpLen() const
{
    return ::ntohs(head.ipLen);
}

void InetIpv4::setIpLen(const u_short &value)
{
    head.ipLen = ::htons(value);
}

u_short InetIpv4::getIpId() const
{
    return ::ntohs(head.ipId);
}

void InetIpv4::setIpId(const u_short &value)
{
    head.ipId = ::htons(value);
}

u_short InetIpv4::getIpOff() const
{
    return ::ntohs(head.ipOff);
}

void InetIpv4::setIpOff(const u_short &value)
{
    head.ipOff = ::htons(value);
}

u_int8_t InetIpv4::getIpP() const
{
    return head.ipP;
}

void InetIpv4::setIpP(const u_int8_t &value)
{
    head.ipP = value;
}

u_short InetIpv4::getIpSum() const
{
    return head.ipSum;
}

void InetIpv4::setIpSum(const u_short &value)
{
    head.ipSum=value;
}

in_addr InetIpv4::getIpSrc() const
{
    return head.ipSrc;
}

void InetIpv4::setIpSrc(const in_addr &value)
{
    head.ipSrc = value;
}

in_addr InetIpv4::getIpDst() const
{
    return head.ipDst;
}

void InetIpv4::setIpDst(const in_addr &value)
{
    head.ipDst = value;
}

PackType InetIpv4::getData() const
{
    return data;
}

void InetIpv4::setData(const PackType &value)
{
    data = value;
}

size_t InetIpv4::getLENGTH()
{
    return sizeof(struct head);
}

}


